package GUI;
import control.*;

import model.*;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Font;

import javax.swing.JLabel;

import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPasswordField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;

@SuppressWarnings({ "serial", "unused", "deprecation" })
public class logIn extends JFrame {

	private JPanel contentPane;
	private static JTextField textFieldUser;
	private JPasswordField passwordField;
	private PersonCtrl personCtrl = new PersonCtrl();
	private mainMenu mm = new mainMenu();
	private static logIn frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new logIn();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public logIn() {
		setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Andrada\\Desktop\\images.jpg"));
		setTitle("LOG IN");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 561, 304);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		textFieldUser = new JTextField();
		textFieldUser.setFont(new Font("Times New Roman", Font.PLAIN, 18));
		textFieldUser.setBounds(229, 75, 153, 33);
		contentPane.add(textFieldUser);
		textFieldUser.setColumns(10);
		
		JButton logInButton = new JButton("Log in");
		logInButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(textFieldUser.getText().equals("") || passwordField.getText().equals("")) {
					JOptionPane.showMessageDialog(null,"You must enter both Username and Password");
					return;					
				}
				Employee emp = personCtrl.getEmployeeByCpr(textFieldUser.getText());
				if(emp == null || !emp.getPassword().equals(passwordField.getText()) || emp.getStatus().equals("Disabled"))
				{
					JOptionPane.showMessageDialog(null,"Username or Password is incorrect");
					return;	
				}
				if(emp.getCpr().equals(textFieldUser.getText()) && emp.getPassword().equals(passwordField.getText()))
				{
					mm.setLogInM(frame);
					mm.setLoggedIn(textFieldUser.getText());
					mm.setAccessTypeVariable(emp.getAccessType());
					mm.setVisible(true);
					passwordField.setText("");
					setVisible(false);
				}
				
			}
		});
		logInButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
			}
		});
		logInButton.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		logInButton.setBounds(410, 209, 106, 33);
		contentPane.add(logInButton);
		
		JLabel lblUser = new JLabel("Username :");
		lblUser.setHorizontalAlignment(SwingConstants.RIGHT);
		lblUser.setFont(new Font("Times New Roman", Font.BOLD, 20));
		lblUser.setBounds(94, 74, 125, 33);
		contentPane.add(lblUser);
		
		JLabel lblPassword = new JLabel("Password :");
		lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPassword.setFont(new Font("Times New Roman", Font.BOLD, 20));
		lblPassword.setBounds(126, 142, 93, 33);
		contentPane.add(lblPassword);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(229, 145, 153, 33);
		contentPane.add(passwordField);
	}
	
	public void setUsername(String _username)
	{
		textFieldUser.setText(_username);
	}
}
